package cn.mesmile.flow.config.listen;

import cn.mesmile.flow.listen.MyEventListener;
import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
import org.flowable.common.engine.api.delegate.event.FlowableEventDispatcher;
import org.flowable.spring.SpringProcessEngineConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Configuration;

/**
 * @author zb
 * @date 2020/12/25 11:44
 * @Description flowable全局的监听器
 */
@Slf4j
@Configuration
public class FlowableGlobListenerConfig implements ApplicationListener<ApplicationReadyEvent> {

    @Autowired
    private SpringProcessEngineConfiguration configuration;

    @Autowired
    private GlobalTaskCreateListener globalTaskCreateListener;
    @Autowired
    private GlobalTaskCompleteListener globalTaskCompleteListener;
    @Autowired
    private GlobalProcessListener globalProcessListener;
    @Autowired
    private MyEventListener myEventListener;

    /**
     *  {@link FlowableEngineEventType}
     *
     * 引擎中所有的 ENTITY_\* 事件都与实体关联。下表列出每个实体分发的实体事件：
     *
     * ENTITY_CREATED, ENTITY_INITIALIZED, ENTITY_DELETED: 附件(Attachment)，备注(Comment)，部署(Deployment)，执行(Execution)，组(Group)，身份关联(IdentityLink)，作业(Job)，模型(Model)，流程定义(ProcessDefinition)，流程实例(ProcessInstance)，任务(Task)，用户(User)。
     *
     * ENTITY_UPDATED: 附件，部署，执行，组，身份关联，作业，模型，流程定义，流程实例，任务，用户。
     *
     * ENTITY_SUSPENDED, ENTITY_ACTIVATED: 流程定义，流程实例/执行，任务。
     *
     *  EmailListener emailListener;
     */

    /**
     *  flow事件类型
     * {@link FlowableEngineEventType}
     * @param applicationReadyEvent
     */
    @Override
    public void onApplicationEvent(ApplicationReadyEvent applicationReadyEvent) {
        FlowableEventDispatcher dispatcher = configuration.getEventDispatcher();
        // 全局任务创建监听器                 【监听器，事件类型】   指定类型事件发生就会就入对应的【监听器】
        dispatcher.addEventListener(globalTaskCreateListener, FlowableEngineEventType.TASK_CREATED);
        dispatcher.addEventListener(globalTaskCompleteListener, FlowableEngineEventType.TASK_COMPLETED);
        dispatcher.addEventListener(globalProcessListener);
        dispatcher.addEventListener(myEventListener);
    }


}
